package com.zhazha.earlybird.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.zhazha.earlybird.beans.CampusRecruitInfo;
import com.zhazha.earlybird.beans.ParttimeJobInfo;

public class CollectParttimeJobDAO {
	private SQLiteDatabase mDatabase;
	
	private static final String COLLECT_PARTTIME_JOB_TABLE = "collect_parttime_job";
	
	private static final String PARTTIME_JOB_ID = "id"; 
	private static final String PARTTIME_JOB_TITLE = "title";
	private static final String PARTTIME_JOB_KIND_ID = "kind_id";
	private static final String PARTTIME_JOB_CITY_ID = "city_id";
	private static final String PARTTIME_JOB_HIT_COUNT = "hit_count";
	
	public CollectParttimeJobDAO(SQLiteDatabase datebase) {
		this.mDatabase = datebase;
	}
	
	public ParttimeJobInfo queryById(int id) {
		ParttimeJobInfo info = null;
		
		try{
			String selection = "id=?";
			String[] selectionArgs = {String.valueOf(id)};
			Cursor c = this.mDatabase.query(COLLECT_PARTTIME_JOB_TABLE, null, selection, selectionArgs, null, null, null);
			if(c.moveToFirst()) {
				
				info = new ParttimeJobInfo();
				
				info.setId(c.getInt(c.getColumnIndex(PARTTIME_JOB_ID)));
				info.setCityId(c.getInt(c.getColumnIndex(PARTTIME_JOB_CITY_ID)));
				info.setHitCount(c.getInt(c.getColumnIndex(PARTTIME_JOB_HIT_COUNT)));
				info.setKindId(c.getInt(c.getColumnIndex(PARTTIME_JOB_KIND_ID)));
				info.setTitle(c.getString(c.getColumnIndex(PARTTIME_JOB_TITLE)));
				
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return info;
		
	}
	
	public ParttimeJobInfo[] queryAll() {
		ParttimeJobInfo[] info = null;
		
		try {
			
			 Cursor c = this.mDatabase.query(COLLECT_PARTTIME_JOB_TABLE, null, null, null, null, null, "id asc");
			
			if(c.moveToFirst()) {
				info = new ParttimeJobInfo[c.getCount()];
				for (int i = 0; i < c.getCount(); i++) {
					
					info[i] = new ParttimeJobInfo();
					info[i].setId(c.getInt(c.getColumnIndex(PARTTIME_JOB_ID)));
					info[i].setTitle(c.getString(c.getColumnIndex(PARTTIME_JOB_TITLE)));
					info[i].setCityId(c.getInt(c.getColumnIndex(PARTTIME_JOB_CITY_ID)));
					info[i].setKindId(c.getInt(c.getColumnIndex(PARTTIME_JOB_KIND_ID)));
					info[i].setHitCount(c.getInt(c.getColumnIndex(PARTTIME_JOB_HIT_COUNT)));

					c.moveToNext();
				}
				return info;
				
			}
			
			return info;
		}
		catch (Exception e) {
			e.printStackTrace();
			return info;
		}
	}
	
	public void deleteAll() {
		
		try{
			this.mDatabase.delete(COLLECT_PARTTIME_JOB_TABLE, null, null);
		}
		catch( Exception e) {
			e.printStackTrace();
		}
	}
	
	public void insert(ParttimeJobInfo info) {
		
		try{
				ContentValues cv = new ContentValues();
				cv.put(PARTTIME_JOB_ID, info.getId());
				cv.put(PARTTIME_JOB_TITLE, info.getTitle());
				cv.put(PARTTIME_JOB_HIT_COUNT, info.getHitCount());
				cv.put(PARTTIME_JOB_CITY_ID, info.getCityId());
				cv.put(PARTTIME_JOB_KIND_ID, info.getKindId());

				this.mDatabase.insert(COLLECT_PARTTIME_JOB_TABLE, null, cv);
		}
		catch( Exception e){
			e.printStackTrace();
		}
	}
	
	public void delete(int id) {
		
		try {
			
			String whereClause = "id=?";
			String[] whereArgs = {String.valueOf(id)};
			this.mDatabase.delete(COLLECT_PARTTIME_JOB_TABLE, whereClause, whereArgs);
		}
		catch ( Exception e) {
			e.printStackTrace();
		}
	}

}
